var pnum = "";
var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findFormulaVersion",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	if(data != null){
		        	var pageCount = data.pager.pageCount;
		        	var dataObj ={
		        			"list":data.list,
		         			"dataTool":{
		         				"title":{
		          			      	"formula_table_code": "公式版本库编号"
		          			      	,"formula_table_name": "公式版本库名称"
		          			      	,"formula_table_version_number":"版本编号"
		          			      	,"project_design_phase_name":"所属设计阶段名称"
		          			      	,"project_design_content_name":"所属设计内容名称"
		          			      	,"project_design_major_name":"所属专业名称"
		          			      	,"create_time":"创建时间"
		          			      	,"update_time":"修改时间"
		          			      	,"operation":"操作"
		            			},
		            			"titleCheckbox":false,
								"btn":[{"event":"updateObj","name":"编辑"},
								       {"event":"updateFormulaList","name":"编辑公式集信息"},
								       {"event":"sortRule","name":"编辑排序规则"},
								       {"event":"delObj","name":"删除"}]
							}
			  		};
		        	showPageTable('view', dataObj,num,pageCount,"initData");
	        	}

	        },
	        error: function () {
	        }
	    });
 }
   
   //设置排序规则
   var sortRule = function(obj){
	   var url = base+"/dataCenter/businessdata/designFormulaSort/"+obj.id+"?name=公式排序";
       $.get(url, function(result){
		   $("#content").html("");
		   $("#content").html(result);
	   });
   }
   
   //校验数据
   var verify_data = function(data){
		if(data.project_design_phase_id == ""){
			alert("请选择设计阶段!");
			return false;
		}
		if(data.project_design_major_id == ""){
			alert("请选择设计专业");
			return false;
		}
		if(data.project_design_content_id == ""){
			alert("请选择设计内容!");
			return false
		}
		return true;
   }

   var searchObj = function (num) {
       if (num==undefined|| num==""||num.type=="click" ) {
           num=1;
       }
       var name=$("#searchName").val();
       var param = {
           page_num:num,
           page_size:"10",
           formula_table_name:name
       }
       $.ajax({
           url: base + "/dataCenter/businessdata/findFormulaVersion",
           data: param,
           success: function (data, textStatus, jqXHR) {
               if(data != null){
                   var pageCount = data.pager.pageCount;
                   var dataObj ={
                       "list":data.list,
                       "dataTool":{
                           "title":{
                               "formula_table_code": "公式版本库编号"
                               ,"formula_table_name": "公式版本库名称"
                               ,"formula_table_version_number":"版本编号"
                               ,"orderBy":"排序规则"
                               ,"project_design_phase_name":"所属设计阶段名称"
                               ,"project_design_content_name":"所属设计内容名称"
                               ,"project_design_major_name":"所属专业名称"
                               ,"create_time":"创建时间"
                               ,"update_time":"修改时间"
                               ,"operation":"操作"
                           },
                           "titleCheckbox":false,
                           "btn":[{"event":"updateObj","name":"编辑"},
                               {"event":"updateFormulaList","name":"编辑公式集信息"},
                               {"event":"delObj","name":"删除"}]
                       }
                   };
                   showPageTable('view', dataObj,num,pageCount,"searchObj");
               }

           },
           error: function () {
           }
       });
   }

   //维护公式库表单信息
   var updateFormulaList = function(obj){
	   var type = $.trim($("#sign_code").val());
	   var url = base+"/dataCenter/businessdata/findFormulaInfoListById/"+obj.id;
	   $.get(url, function(result){
		   $("#content").html("");
		   $("#content").html(result);
	   });
   }
   
   //添加公式库版本
   var addFormulaVersion = function(){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "formula_table_code":"公式库编号",
					   "formula_table_name":"公式库名称",
					   "project_design_phase_id":"设计阶段",
					   "project_design_major_id":"设计专业",
					   "project_design_content_id":"设计内容"
				   },"text":{
					   "formula_table_code":"",
					   "formula_table_name":"",
					   "project_design_phase_id":"",
					   "project_design_major_id":"",
					   "project_design_content_id":""
				   },"input":{
					   "formula_table_code":"text",
					   "formula_table_name":"text",
					   "project_design_phase_id":"select",
					   "project_design_major_id":"new_select",
					   "project_design_content_id":"select"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
	   initPhase();
	   initDesignMajor();
	   chanageMajorselect();
   }
   
   //监听专业下拉选
   var chanageMajorselect = function(){
   		form.on("select(new_select)",function(value){
   			var major_id = value.value;
   			initContent(major_id);
   		});
   }
   
   var initContent = function(major_id){
 	   $.post(base+"/dataCenter/businessdata/findDesignContent",{major_id:major_id},
 	   		  	function(data,status){
 	     		    	if(data!=''){
 	   	      		  	 var optionstring = "";
 	   	                 $.each(data.list, function(i,item){
 	   	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_content_name + "</option>";
 	   	                });
 	   	                $("select[name='project_design_content_id']").html('<option value=""></option>' + optionstring);
 	   	      		}else{
 	   	      		  	$("select[name='project_design_content_id']").html('<option value=""></option>');
 	   	      		}
 	     		    form.render(); 
 	  });
   }
    
    var initDesignMajor = function(){
 	   $.post(base+"/dataCenter/businessdata/findDesignMajorData",{},
    		  	function(data,status){
      		    	if(data!=''){
    	      		  	 var optionstring = "";
    	                 $.each(data.list, function(i,item){
    	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
    	                });
    	                $("select[name='project_design_major_id']").html('<option value=""></option>' + optionstring);
    	      		}else{
    	      		  	$("select[name='project_design_major_id']").html('<option value=""></option>');
    	      		}
      		    form.render(); 
   });
   }
    
    var initPhase = function(){
 	   $.post(base+"/dataCenter/businessdata/findDesignPhaseData",{},
 	   		  	function(data,status){
     		    	if(data != null && data.list != null){
   	      		  	 var optionstring = "";
   	                 $.each(data.list, function(i,item){
   	                	optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_phase_name + "</option>";
   	                });
   	                $("select[name='project_design_phase_id']").html('<option value=""></option>' + optionstring);
   	      		}else{
   	      		  	$("select[name='project_design_phase_id']").html('<option value=""></option>');
   	      		}
     		    form.render(); 
 	  });
    }
   
   
   //编辑公式库版本
   var updateObj = function(obj){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "id":"主键",
					   "project_design_phase_id":"所属设计阶段",
					   "project_design_content_id":"所属设计内容",
					   "project_design_major_id":"所属设计专业",
					   "formula_table_code":"公式库编号",
					   "formula_table_name":"公式库名称"
				   },"text":{
					   "id":obj.id,
					   "project_design_phase_id":obj.project_design_phase_name,
					   "project_design_content_id":obj.project_design_content_name,
					   "project_design_major_id":obj.project_design_major_name,
					   "formula_table_code":obj.formula_table_code,
					   "formula_table_name":obj.formula_table_name
				   },"input":{
					   "id":"hidden",
					   "project_design_phase_id":"text_readonly",
					   "project_design_content_id":"text_readonly",
					   "project_design_major_id":"text_readonly",
					   "formula_table_code":"text",
					   "formula_table_name":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
   }
   
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id}
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delFormulaVersionById",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   var layopen = function(content){
		layer.open({
		  	title:'更新公式版本库'
			,content: content
	    	,area: ['800px', '600px']
		    ,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
  		  	    var data = $(layero).find("form").serializeJSON();
				if(data.formula_table_code == ""){
					var obj = $("input[name='formula_table_code']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				if(data.formula_table_name == ""){
					var obj = $("input[name='formula_table_name']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				if(data.project_design_phase_id == ""){
					alert("请选择设计阶段!");
					return false;
				}
				if(data.project_design_major_id == ""){
					alert("请选择设计专业");
					return false;
				}
				if(data.project_design_content_id == ""){
					alert("请选择设计内容!");
					return false
				}
				$.post(base+"/dataCenter/businessdata/updateFormulaVersion",{
					id:data.id==null?"":data.id,
					formula_table_code:$.trim(data.formula_table_code),
					formula_table_name:$.trim(data.formula_table_name),
					project_design_phase_id:data.project_design_phase_id,
					project_design_major_id:data.project_design_major_id,
					project_design_content_id:data.project_design_content_id
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
			}
	});
  }
   $(document).ready(function(){
	   initData(1);
	   $("#addData").click(addFormulaVersion);
       $("#searchInfo").click(searchObj);
       $("#sortRule").click(sortRule);
   });